package cn.com.huzo.service.trade.impl;

import cn.com.huzo.model.entities.trade.SalesContractLinesEntity;
import cn.com.huzo.repository.trade.SalesContractLinesRepository;
import huzofw.service.impl.GenericManagerImpl;
import cn.com.huzo.service.trade.SalesContractLinesManager;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 售货合同明细管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("SalesContractLinesManager")
public class SalesContractLinesManagerImpl extends GenericManagerImpl<SalesContractLinesEntity, String>
        implements SalesContractLinesManager {

    @Resource(name = "SalesContractLinesRepository")
    private SalesContractLinesRepository _dao;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public List<SalesContractLinesEntity> fetchLinesByHeaderKey(Long headerId) throws Exception {
        return _dao.fetchByHeaderId(headerId);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchUpdate(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();
        Integer rowsAffected1 = 0;
        Integer rowsAffected2 = 0;

        ObjectMapper mapper = new ObjectMapper();
        List<SalesContractLinesEntity> insertList = mapper.convertValue(MapUtils.getObject(criterion, "InsertList"), new TypeReference<List<SalesContractLinesEntity>>() {
        });
        List<SalesContractLinesEntity> updateList = mapper.convertValue(MapUtils.getObject(criterion, "UpdateList"), new TypeReference<List<SalesContractLinesEntity>>() {
        });

        if (!insertList.isEmpty()) {
            rowsAffected1 = _dao.batchInsert(insertList);
        }
        if (!updateList.isEmpty()) {
            rowsAffected2 = _dao.batchUpdate(updateList);
        }

        // 审计日志
        if ((rowsAffected1 + rowsAffected2) > 0) {
            // 记录日志
            //writeAuditLog(itemList.get(0).getUpdatedBy(), "采购管理→非材料花费明细",
            //        "款号：" + _header.getStyleNo(),
            //        "更新");
            _result.put("Success", true);
        } else {
            _result.put("Success", false);
        }

        _result.put("LinesList", _dao.fetchByHeaderId(MapUtils.getLong(criterion, "HeaderId")));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        Integer rowsAffected = _dao.delete(MapUtils.getString(criterion, "LinesId"));
        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"), "贸易信息→售货合同",
                    "明细信息：" + MapUtils.getString(criterion, "DeletedLinesInfo"),
                    "删除");
        }
        Map<String, Object> _result = new HashMap();
        _result.put("LinesList", _dao.fetchByHeaderId(MapUtils.getLong(criterion, "HeaderId")));

        return _result;
    }
}
